#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
int N, M;
typedef pair<int, int> pii;
vector<int> G[maxn];

int topo[maxn], vis[maxn], pos;

bool TOPO(int u){
//    if(G[u].empty()) {topo[pos--] = u; return true;}
    vis[u] = -1;
    for (auto v:G[u]) {
        if(vis[v] == -1) return false;
        if(!vis[v] && !TOPO(v)) return false;
    }
    vis[u] = 1;
    topo[pos--] = u;
    return true;
}
int main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    int T; cin >> T;
    while(T--){
        cin >> N >> M;
        memset(G, 0, sizeof(G));
        vector<pii> edge;
        for (int i = 0; i < M; ++i) {
            int c, a, b; cin >> c >> a >> b;
            if(c) G[a].push_back(b);
            edge.emplace_back(a, b);
        }

        memset(vis, 0, sizeof(vis));
        pos = N;
        bool circle = false;
        for (int i = 1; i <= N; ++i) {
            if(!vis[i]) if(!TOPO(i)) {circle = true; break;}
        }
        if(circle) {cout << "NO\n"; continue;}
        else cout << "YES\n";
        int P[N+1];
        for (int i = 1; i <= N; ++i) {
            P[topo[i]] = i;
        }

        for(auto e:edge){
            int u = e.first, v = e.second;
            if(P[u] < P[v]) cout << u << ' ' << v << "\n";
            else cout << v << ' ' << u << "\n";
        }


    }
    return 0;
}